﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using Easy.Common;
using Easy.Plugin.Interface;
using Microsoft.Practices.ServiceLocation;

namespace Easy.Plugin.Managers
{
    class ExportServiceLocatorPluginManager : PluginRootManager
    {
        [Import]
        Lazy<IExportServiceLocatorPlugin> _serviceLocatorPlugin;
        private PluginRootManager _pluginManager;


        public ExportServiceLocatorPluginManager(PluginRootManager pluginManager)
            : base(pluginManager.PluginData)
        {
            _pluginManager = pluginManager;
        }

        public override void Run()
        {
            RunParent();

            RunMe();
        }


        #region private

        private void RunParent()
        {
            _pluginManager.Run();

            _pluginManager.Container.ComposeParts(this);

            base.Container = _pluginManager.Container;
        }

        private void RunMe()
        {
            if (_serviceLocatorPlugin == null)
            {
                return;
            }

            IServiceLocator serviceLocator = _serviceLocatorPlugin.Value.Export();

            CheckHelper.NotNull(serviceLocator, "serviceLocator");

            AddPluginData(typeof(IServiceLocator), serviceLocator);
        }

        #endregion
    }
}
